/**
 * 服务器服务路径
 * chrome open
 * open -a 'Google Chrome' --args -allow-file-access-from-files
 *  --disable-web-security
 */
var GLOBAL_CONTEXT_PATH = 'http://192.168.1.37:8080/xone-app';
//var GLOBAL_CONTEXT_PATH = 'http://localhost:8080/xone-app';

/**
 * 
 */
var PAGE_SLIDER = new PageSlider($("#container"));

/**
 * 页面对应的标题常量
 */
var HEADER_NAME = {};
HEADER_NAME['guidepage'] = '欢迎使用';
HEADER_NAME['marketpage'] = '逛逛市场';
HEADER_NAME['parterpage'] = '我的伙伴';
HEADER_NAME['userpage'] = '用户中心';
HEADER_NAME['details'] = '产品详细';
HEADER_NAME['productdetails'] = '产品详细';
HEADER_NAME['productlist'] = '产品列表';
HEADER_NAME['productsalelist'] = '促销列表';
HEADER_NAME['productgrouplist'] = '组团列表';
HEADER_NAME['productcreate'] = '发布产品';
HEADER_NAME['userlogin'] = '用户登录';
HEADER_NAME['userregister'] = '用户注册';
HEADER_NAME['linkspage'] = '定制快捷方式';
HEADER_NAME['purchasecreate'] = '求购发布';
HEADER_NAME['userinfo'] = '我的资料';
HEADER_NAME['deliverycreate'] = '物流配送';
HEADER_NAME['deliverylist'] = '物流配送列表';
HEADER_NAME['purchaselist'] = '求购列表';
HEADER_NAME['purchasedetails'] = '求购详情';
HEADER_NAME['userproductlist'] = '产品详情';
HEADER_NAME['userproductupdate'] = '产品编辑';
HEADER_NAME['companyinfo'] = '公司详情';
HEADER_NAME['useradvertisementlist'] = '用户广告列表';
HEADER_NAME['useradvertisementinfo'] = '用户广告详情';
HEADER_NAME['useroverheadlist'] = '用户顶置列表';
HEADER_NAME['userproductdetails'] = '用户产品详情';
HEADER_NAME['useroverheaddetails'] = '顶置申请详情';
HEADER_NAME['usersubscribecreate'] = '筛选订阅';
HEADER_NAME['usersubscribelist'] = '用户订阅列表';
HEADER_NAME['usersubscribedetails'] = '用户订阅详情';
HEADER_NAME['usersubscribeedit'] = '编辑订阅';
HEADER_NAME['usersubscribeproductlist'] = '产品订阅列表';
HEADER_NAME['userproductgrouplist'] = '用户组团列表';
HEADER_NAME['userproductgroupdetails'] = '用户组团详情';
//HEADER_NAME[''] = '';

/**
 * Handlebars常量定义
 */
Handlebars.registerHelper("HB_GLOBAL_CONTEXT_PATH", function() {
	return GLOBAL_CONTEXT_PATH;
});

/**
 * 从数组中取第index-1个元素
 */
Handlebars.registerHelper("HB_GET_AT_INDEX", function(array, index) {
	try {
		return array[index];
	} catch (e) {
		return -1;
	}
});

/**
 * 从数组中取第index-1个元素
 */
Handlebars.registerHelper("PRODUCT_CREATE_BY_SALETYPE", function(v) {
	if (v == '1') {
		return '促销发布';
	} else if (v == '2') {
		return '组团发布';
	} else if (v == '0') {
		return '产品发布';
	}
	return '未知的发布';
});

/**
 * 从数组中取第index-1个元素
 */
Handlebars.registerHelper("PRODUCT_LIST_BY_SALETYPE", function(v) {
	if (v == '1') {
		return '促销列表';
	} else if (v == '2') {
		return '组团列表';
	} else if (v == '0') {
		return '产品列表';
	}
	return '未知的列表';
});
/**
 * 从数组中取第index-1个元素
 */
Handlebars.registerHelper("PRODUCT_LIST_STATUS_COLOR", function(v) {
	if (v == '0') {
		return '#FCD102';
	} else if (v == '1') {
		return 'green';
	}
	return 'red';
});
/**
 * 根据广告类型取对应的广告地址
 */
Handlebars.registerHelper("HREF_BY_ADTYPE", function(v, id) {
	if (v == '0') {
		return '#productdetails&id=' + id;
	} else if (v == '1') {
		return '#purchasedetails&id=' + id;
	} else if (v == '2') {
		return '#companyinfo&id=' + id;
	}
	return 'javascript:void(0)';
});

/**
 * 从索引中取值对3取模
 */
Handlebars.registerHelper("HB_MOD3_IF_INDEX", function(index, opt) {
	var i = parseInt(index, 10);
	if (i != 0 && i % 3 == 0) {
		return opt.fn(this);
	} else {
		return opt.inverse(this);
	}
});
/**
 * 表达式比较
 */
Handlebars.registerHelper('HB_COMPARE', function (v1, operator, v2, options) {
    switch (operator) {
        case '==':
            return (v1 == v2) ? options.fn(this) : options.inverse(this);
        case '===':
            return (v1 === v2) ? options.fn(this) : options.inverse(this);
        case '<':
            return (v1 < v2) ? options.fn(this) : options.inverse(this);
        case '<=':
            return (v1 <= v2) ? options.fn(this) : options.inverse(this);
        case '>':
            return (v1 > v2) ? options.fn(this) : options.inverse(this);
        case '>=':
            return (v1 >= v2) ? options.fn(this) : options.inverse(this);
        case '!=':
        	return (v1 != v2) ? options.fn(this) : options.inverse(this);
        default:
            return options.inverse(this);
    }
});
/**
 * 日期格式化
 */
Handlebars.registerHelper('HB_FORMAT_DATE', function (param) {
	if (typeof param == 'string') {
		param = param.replace(/T/ig, ' ');
		param = param.replace(/\.0$/, '');
		return new Handlebars.SafeString(param);
	}
	return '';
});

/**
 * Handlebars Helper Test
 */
Handlebars.registerHelper("HANDLEBARS_EXPR", function(predicate, options) {
	var declarations = '';
	for (var field in this) {
		console.log('field in this:' + field);
		declarations += field + ' = this.' + field + ',';
	}
	if (eval(declarations + predicate)) {
		return options.fn(this);
	}
//	console.log('params=' + JSON.stringify(params));
//	try {
//		var o = params.join(' ');
//		console.log('o:' + o);
//		var p = eval(o);
//		console.log('p=' + p);
//		var v = new Handlebars.SafeString(p);
//		console.log('v=' + v);
//		return v;
//	} catch (e) {
//		console.log(e.message);
//	}
});
